package com.wlyuan.index.domain.source.utils;

import com.wlyuan.index.api.dto.QuotationIndexDTO;
import com.wlyuan.index.domain.source.valueobj.Character;
import com.wlyuan.index.domain.source.valueobj.*;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.BooleanUtils;

import java.util.Optional;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SourceUtils {
    private static final Integer TRUE = 1;

    public static Character createCharacter(Integer value) {
        return Character.valueOf(value);
    }

    public static BalanceBit createBalanceBit(Integer value) {
        return new BalanceBit(value);
    }

    public static CustomerType createCustomerType(Integer value) {
        return CustomerType.valueOf(value);
    }

    public static CarrierType createCarrierType(Integer value) {
        return CarrierType.valueOf(value);
    }

    public static WaybillStatus createWaybillStatus(Integer value) {
        return WaybillStatus.valueOf(value);
    }

    public static BalanceStatus createBalanceStatus(Integer value) {
        return BalanceStatus.valueOf(value);
    }

    public static QuotationStatus createQuotationStatus(Integer value) {
        return QuotationStatus.valueOf(value);
    }

    public static BillStatus createBillStatus(Integer value) {
        return BillStatus.valueOf(value);
    }

    public static SignType createSignType(Integer value) {
        return SignType.valueOf(value);
    }

    public static ReceiptType createReceiptType(Integer value) {
        return ReceiptType.valueOf(value);
    }

    public static Highlight createHighlight(Integer value) {
        return Highlight.valueOf(value);
    }

    public static QuotationNo createQuotationNo(String value) {
        return Optional.ofNullable(value)
                .map(QuotationNo::new)
                .orElse(null);
    }

    public static CombineNo createCombineNo(String value) {
        return Optional.ofNullable(value)
                .map(CombineNo::new)
                .orElse(null);
    }

    public static BillNo createBillNo(String value) {
        return Optional.ofNullable(value)
                .map(BillNo::new)
                .orElse(null);
    }

    public static Boolean intToBool(Integer value) {
        return Optional.ofNullable(value)
                .map(TRUE::equals)
                .orElse(null);
    }

    public static Integer boolToInt(Boolean value) {
        return Optional.ofNullable(value)
                .map(b -> BooleanUtils.isTrue(b) ? 1 : 0)
                .orElse(null);
    }

    public static boolean isWaybillQuote(QuotationIndexDTO quotation) {
        return QuotationIndexDTO.WAYBILL.equals(quotation.getSourceType());
    }

    public static boolean isCombineQuote(QuotationIndexDTO quotation) {
        return QuotationIndexDTO.COMBINE.equals(quotation.getSourceType());
    }
}
